﻿using Engine;
using Game;
using Game.NetWork;
using Game.NetWork.Packages;
using Game.Server;
using Game.Server.Event;
using Game.Server.PlayerEvent;
using GameEntitySystem;
using MessagePack;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using XmlUtilities;
using System.Xml.Linq;
using System.Xml;
using System.Web;
using MessagePack.Formatters;

namespace Game.Server.Plugins
{
    public class 吃东西回血插件 : ServerPlugin, IPlayerInventoryHandle
    {
        public override string Name => "吃东西回血插件";
        public override int Version => 100;

        byte IPlayerInventoryHandle.FirstLevel => 1;

        public override void Initialize()
        {
            PlayerInventoryEventManager.AddObject(this);
            Log.Write(LogType.Error, "吃东西回血插件已初始化");
        }

        // 意义不明，待验证
        public bool PlayerDrop(ComponentPlayer componentPlayer)
        {
            return true; // 允许默认处理
        }

        // 扔掉背包物品时执行
        public bool PlayerDrapDrop(ComponentPlayer componentPlayer, Vector3 worldPos, InventoryDragData inventoryDragData, int count)
        {
            return true; // 允许默认处理
        }

        public void cc(ComponentPlayer componentPlayer)
        {
            componentPlayer.ComponentHealth.Health += 0.3f;
            CommonLib.Net.QueuePackage(new ComponentHealthPackage(componentPlayer.ComponentHealth));
        }

        // 移动背包物品时执行
        public bool PlayerHandleDragDrop(ComponentPlayer componentPlayer, IInventory sourceInventory, int sourceSlotIndex, DragMode dragMode, IInventory targetInventory, int targetSlotIndex, bool processingOnly)
        {
            // 这里可能是处理拖拽操作的地方
            if (sourceInventory != null && targetInventory != null && sourceInventory != targetInventory)
            {
                int sourceItemID = componentPlayer.ComponentMiner.Inventory.GetSlotValue(sourceSlotIndex);
                if (sourceItemID == 2)// 
                {
                    // 尝试给玩家加血
                    cc(componentPlayer);
                    return true; // 表示已处理
                }
                else
                {
                    Log.Error("吃东西回血插件未处理的拖拽操作");
                }
            }
            return true;
        }

        // 受理移动的背包物品时执行 - 这个最可能是处理物品移动的地方
        public bool PlayerHandleMoveItem(ComponentPlayer componentPlayer, IInventory sourceInventory, int sourceSlotIndex, IInventory targetInventory, int targetSlotIndex, int count)
        {
            // 检查是否是有效的物品移动操作
            //if (sourceInventory != null && targetInventory != null && sourceInventory != targetInventory)
            //{
            //    // 给玩家加血
            //    cc(componentPlayer);
            //    return true; // 表示已处理
            //}
            return true;
        }

        public override void Load()
        {

        }

        public override void Save()
        {

        }
    }
}